//
//  FGFileMessageCellData.m
//  FGChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "FGFileMessageCellData.h"
#import "UIColor+FGChat.h"
#import "UIImage+FGUtil.h"
#import "FGChatHelper.h"
#import "FGPathTool.h"
#import "FGNetworkHelper.h"

@implementation FGFileMessageCellData

- (instancetype)initWithDirection:(FGMsgDirection)direction {
    self = [super initWithDirection:direction];
    if (self) {

        if (direction == FGMsgDirectionIncoming) {
            self.bubble = [UIImage imageReceiverWithColor:[UIColor fg_colorWithLightColor:[UIColor whiteColor] darkColor:[UIColor fg_colorWithString:@"#333333"]]];
        } else {
            self.bubble = [UIImage imageSenderWithColor:[UIColor fg_colorWithLightColor:[UIColor whiteColor] darkColor:[UIColor fg_colorWithString:@"#333333"]]];
        }

        _fileNameFont = [UIFont systemFontOfSize:15];
        _fileNameColor = [UIColor fg_colorWithLightColor:[UIColor blackColor] darkColor:[UIColor whiteColor]];
        
        _lengthFont = [UIFont systemFontOfSize:12];
        _lengthColor = [UIColor fg_colorWithLightColor:[UIColor grayColor] darkColor:[UIColor grayColor]];
        
        _flagFont = [UIFont systemFontOfSize:12];
        _flagColor = [UIColor fg_colorWithLightColor:[UIColor lightGrayColor] darkColor:[UIColor lightGrayColor]];
        
        _lineColor = [UIColor fg_colorWithLightColor:[UIColor fg_colorWithString:@"#DDDDDD"] darkColor:[UIColor fg_colorWithString:@"#555555"]];
        
    }
    return self;
}

+ (NSString *)reuseIdentifier {
    return @"FGFileMessageCell";
}

- (CGSize)contentSize {
    if (self.flagName.length == 0) {
        CGFloat minHeight = 70;
        return CGSizeMake([UIScreen mainScreen].bounds.size.width * 0.6, MAX([UIScreen mainScreen].bounds.size.height * 0.09, minHeight));
    }
    CGFloat minHeight = 90;
    return CGSizeMake([UIScreen mainScreen].bounds.size.width * 0.6, MAX([UIScreen mainScreen].bounds.size.height * 0.11, minHeight));
}

- (void)downloadFile {
    NSString *path = [self getFilePath];
    BOOL isExist = [FGPathTool isExistFile:path];
    if (isExist) {
        return;
    }
    if (self.isDownloading)
        return;
    self.isDownloading = YES;
        
    __weak typeof(self) ws = self;
    [self downloadFile:path progress:^(NSUInteger progress) {
        ws.downladProgress = progress;
    } succ:^{
        ws.isDownloading = NO;
        dispatch_async(dispatch_get_main_queue(), ^{
            ws.path = path;
        });
    } fail:^(int code, NSString * _Nonnull msg) {
        ws.isDownloading = NO;
    }];
}

- (void)downloadFile:(NSString *)path progress:(FGDownloadProgress)progress succ:(FGSucc)succ fail:(FGFail)fail {
    [FGNetworkHelper downloadWithURL:self.url filePath:path progress:^(int64_t completedCount, int64_t totalCount) {
        CGFloat received = (CGFloat)completedCount;
        NSUInteger pp = (NSUInteger)(received/totalCount * 100);
       
        if (progress) {
            progress(pp);
        }
    } success:^(NSString * _Nonnull filePath) {
        if (succ) {
            succ();
        }
    } failure:^(NSError * _Nonnull error) {
        if (fail) {
            fail(10001, @"server download fail");
        }
    }];
}

- (BOOL)isLocalExist {
    return [FGPathTool isExistFile:[self getFilePath]];
}

- (NSString *)getFilePath {
    if (!self.conversationId) {
        NSAssert(NO, @"必须设置conversationId，方便管理会话文件");
    }
    return [FGChatHelper pathConvFile:self.conversationId name:self.uuid];
}


@end
